package com.yanqu.road.web.controller.game;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.web.HttpJsonWebParam;
import com.yanqu.road.server.helper.SignHelper;
import com.yanqu.road.server.manger.AppKeyMgr;
import com.yanqu.road.utils.string.StringUtils;
import com.yanqu.road.web.controller.BaseJsonHttpServlet;
import com.yanqu.road.webservice.invoke.GameWebServiceInvoke;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.SortedMap;

@WebServlet(urlPatterns = "/queryUserUnionInfo")
public class QueryUserUnionInfo extends BaseJsonHttpServlet {

    private static Logger logger = LogManager.getLogger(QueryUserUnionInfo.class.getName());

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("request QueryUserUnionInfo");
        HttpJsonWebParam httpJsonWebParam = super.parseRequest(request);
        JSONObject paramObj = httpJsonWebParam.getParamObj();
        JSONObject resultObj;
        String platformUserId;
        long userId;
        String sign;
        try{
            platformUserId = paramObj.getString("user_id");
            userId = paramObj.getLongValue("role_id");
            sign = paramObj.getString("sign");
        }catch (Exception e){
            logger.error("QueryUserUnionInfo argument error:" + e);
            sendResult(response, 0, "argument error");
            return;
        }

        try{
//            SortedMap<String, String> paramMap = SignHelper.getParamMap(paramObj);
//            if(!SignHelper.checkSign(paramMap, AppKeyMgr.getAndroidAppSecret(), sign)){
//                logger.error("QueryUserUnionInfo sign error");
//                sendResult(response, 0, "sign error");
//                return;
//            }
            logger.info("QueryUserUnionInfo 请求成功");
            String dataString = GameWebServiceInvoke.queryUserUnionInfo(platformUserId, userId);
            if(StringUtils.isNullOrEmpty(dataString)){
                logger.info("QueryUserUnionInfo no roles");
                sendResult(response, 0, "no roles");
            }else {
                JSONObject dataObj = JSON.parseObject(dataString);
                resultObj = getResultObj(1, "success");
                resultObj.put("data", dataObj);
                logger.info("QueryUserUnionInfo success");
                sendMessage(response, resultObj.toString());
            }

        } catch (Exception e) {
            logger.error("QueryUserUnionInfo error:" + e);
            sendResult(response, 0, "other error");
        }
    }
}
